package com.ticket;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ManagerLogin {
    private static Connection connection = null;
    private static PreparedStatement preparedStatement = null;
    private static ResultSet resultSet = null;
    private static Scanner scanner = new Scanner(System.in);
    private static String date_format = "^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\\d|3[0-1])$";
    private static String num_format = "^[0-9]+$";
    private static String price_format="\\$?\\d+(\\.\\d{1,2})?$";

    private static String DateFormat(){
        String date;
        while (true) {
            date = scanner.next();
            if (date.matches(date_format))
                return date;
            else{
                System.out.println("请注意格式YYYY-MM-DD");
                System.out.print("日期(YYYY-MM-DD):");
            }
        }
    }
    private static String NumberFormat(){
        String number;
        while (true) {
            number = scanner.next();
            if(number.matches(num_format))
                return number;
            else{
                System.out.println("请注意格式,只能输入正整数!");
                System.out.print("票数:");
            }
        }
    }
    private static String PriceFormat(){
        String price;
        while (true) {
            price = scanner.next();
            if(price.matches(price_format))
                return price;
            else{
                System.out.println("请注意格式,至多两位小数!");
                System.out.print("价格:");
            }
        }
    }

    public static void showall() {
        int i = 0;
        try {
            connection = DBUtils.getConnection();

            String sql = "select * from concert";
            preparedStatement = connection.prepareStatement(sql);

            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                if (i == 0)
                    System.out.println("id\t姓名\t\t日期\t\t\t地址\t\t总票数\t余票\t价格");
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String date = resultSet.getString("date");
                String address = resultSet.getString("address");
                int total = resultSet.getInt("total");
                int number = resultSet.getInt("number");
                int price = resultSet.getInt("price");

                System.out.println(id + "\t" + name + "\t" + date + "\t\t" + address + "\t\t" + total + "\t" + number + "\t" + price);
                i++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }
    }

    public static void addticket() {
        String name, address, date,total, number, price;

        System.out.print("歌手:");
        name = scanner.next();
        System.out.print("日期(YYYY-MM-DD):");
        date = DateFormat();
        System.out.print("地址:");
        address = scanner.next();
        System.out.print("总票数:");
        total = NumberFormat();
        System.out.print("余票:");
        number = NumberFormat();
        System.out.print("价格:");
        price = PriceFormat();

        try {
            connection = DBUtils.getConnection();

            String sql = "insert into concert(name,date,address,total,number,price) values(?,?,?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, name);
            preparedStatement.setDate(2, Date.valueOf(date));
            preparedStatement.setString(3, address);
            preparedStatement.setInt(4, Integer.parseInt(total));
            preparedStatement.setInt(5, Integer.parseInt(number));
            preparedStatement.setDouble(6, Double.parseDouble(price));

            int row = preparedStatement.executeUpdate();

            if (row > 0)
                System.out.println("添加成功");
            else
                System.out.println("添加失败");

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }
    }

    public static void modifyticket() { //修改
        String name;
        int i = 0;
        boolean flag=false;
        List<Integer> ids = new ArrayList<>();
        System.out.print("请输入要修改的歌手名字:");
        name = scanner.next();

        try {
            connection = DBUtils.getConnection();

            String sql = "select * from concert where name  like  \'%" + name + "%\' ";
            preparedStatement = connection.prepareStatement(sql);

            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                if (i == 0)
                    System.out.println("id\t姓名\t\t\t日期\t\t\t地址\t\t总票数\t\t余票\t\t价格");
                int Id = resultSet.getInt("id");
                ids.add(Id);
                String username = resultSet.getString("name");
                String date = resultSet.getString("date");
                String address = resultSet.getString("address");
                int total = resultSet.getInt("total");
                int number = resultSet.getInt("number");
                int price = resultSet.getInt("price");

                System.out.println(Id + "\t" + username + "\t\t" + date + "\t" + address + "\t\t" + total + "\t" + number + "\t" + price);
                i++;
            }
            if (i > 0) {
                System.out.print("以上是\"" + name + "\"歌手相关演唱会,请根据id号选择修改:");
                String id = scanner.next();
                for (Integer index : ids) {
                    if(id.equals(index.toString())){
                        flag=true;
                        break;
                    }
                }

                if(flag){
                    while (true) {
                        System.out.println("1.日期\t2.地址\t3.总票数\t4.余票\t5.价格\t0.退出修改");
                        System.out.print("请选择想要修改的信息号:");
                        String j = scanner.next();
                        if (j.equals("1")) {
                            System.out.print("日期(YYYY-MM-DD):");
                            String date = DateFormat();
                            updateDate(id,date);
                        } else if (j.equals("2")) {
                            System.out.print("地址:");
                            String address = scanner.next();
                            updateAddress(id,address);
                        } else if (j.equals("3")) {
                            System.out.print("总票数:");
                            String total = NumberFormat();
                            updateTotal(id,total);
                        } else if (j.equals("4")) {
                            System.out.print("余票:");
                            String number = NumberFormat();
                            updateNumber(id,number);
                        } else if (j.equals("5")) {
                            System.out.print("价格:");
                            String price = PriceFormat();
                            updatePrice(id,price);
                        } else if (j.equals("0")) {
                            break;
                        } else {
                            System.out.println("输入有误，请重新输入");
                        }
                    }
                }else {
                    System.out.println("ID号输入有误!");
                }
            } else
                System.out.println("未查询到\"" + name + "\"歌手相关演唱会");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }
    }

    public static void deleteticket() { //删除
        String name;
        int i = 0;
        List<Integer> ids = new ArrayList<>();
        System.out.print("请输入要删除的歌手名字:");
        name = scanner.next();

        try {
            connection = DBUtils.getConnection();

            String sql = "select * from concert where name like  \'%" + name + "%\' ";
            preparedStatement = connection.prepareStatement(sql);

            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                if (i == 0)
                    System.out.println("id\t姓名\t\t日期\t\t\t地址\t\t总票数\t余票\t价格");
                int Id = resultSet.getInt("id");
                ids.add(Id);
                String username = resultSet.getString("name");
                String date = resultSet.getString("date");
                String address = resultSet.getString("address");
                int total = resultSet.getInt("total");
                int number = resultSet.getInt("number");
                int price = resultSet.getInt("price");

                System.out.println(Id + "\t" + username + "\t" + date + "\t\t" + address + "\t\t" + total + "\t" + number + "\t" + price);
                i++;
            }
            if (i > 0) {
                System.out.print("以上是\"" + name + "\"歌手相关演唱会,请根据id号选择删除:");
                String id = scanner.next();
                boolean flag=false;
                for (Integer index : ids) {
                    if(id.equals(index.toString())){
                        flag=true;
                        break;
                    }
                }

                if(flag) {
                    String sql1 = "delete from concert where id = ?";
                    preparedStatement = connection.prepareStatement(sql1);

                    preparedStatement.setInt(1, Integer.parseInt(id));
                    int row = preparedStatement.executeUpdate();

                    if (row > 0) {
                        System.out.println("删除成功");
                    } else {
                        System.out.println("删除失败");
                    }
                }else{
                    System.out.println("ID号输入有误!");
                }
            } else {
                System.out.println("未查询到\"" + name + "\"歌手相关演唱会");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }
    }

    private static void updateDate(String id,String date){
        try {
            connection = DBUtils.getConnection();

            String sql = "update concert set date = ? where id = ?";
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setDate(1, Date.valueOf(date));
            preparedStatement.setInt(2, Integer.parseInt(id));

            int row = preparedStatement.executeUpdate();
            if (row > 0)
                System.out.println("修改成功");
            else
                System.out.println("修改失败");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }
    }
    private static void updateAddress(String id,String address){
        try {
            connection = DBUtils.getConnection();

            String sql = "update concert set address = ? where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, address);
            preparedStatement.setInt(2, Integer.parseInt(id));

            int row = preparedStatement.executeUpdate();
            if (row > 0)
                System.out.println("修改成功");
            else
                System.out.println("修改失败");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }
    }
    private static void updateTotal(String id,String total){
        try {
            connection = DBUtils.getConnection();

            String sql  = "update concert set total = ? where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, Integer.parseInt(total));
            preparedStatement.setInt(2, Integer.parseInt(id));

            int row = preparedStatement.executeUpdate();
            if (row > 0)
                System.out.println("修改成功");
            else
                System.out.println("修改失败");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }
    }
    private static void updateNumber(String id,String number){
        try {
            connection = DBUtils.getConnection();

            String sql = "update concert set number = ? where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, Integer.parseInt(number));
            preparedStatement.setInt(2, Integer.parseInt(id));
            int row = preparedStatement.executeUpdate();
            if (row > 0)
                System.out.println("修改成功");
            else
                System.out.println("修改失败");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }
    }
    private static void updatePrice(String id,String price){
        try {
            connection = DBUtils.getConnection();

            String sql = "update concert set price = ? where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setDouble(1, Double.parseDouble(price));
            preparedStatement.setInt(2, Integer.parseInt(id));
            int row = preparedStatement.executeUpdate();
            if (row > 0)
                System.out.println("修改成功");
            else
                System.out.println("修改失败");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils.close(resultSet,preparedStatement,connection);
        }
    }
}
